
/*==========================================================================
	Section:	Emulations
==========================================================================*/	
WPP_COLIDE_NORMAL = 0
WPP_COLIDE_SOLID = 1
WPP_COLIDE_GHOST = 2

/*==========================================================================
	Name:		Entity.WPP_SetOwner
	Purpose: 	Sets Entitys Owner
==========================================================================*/	
function _R.Entity:WPP_SetOwner( UniqueID )
	if ( !UniqueID ) then return end
	local Player,Name = player.GetByUniqueID(UniqueID),"Unkown"
	if ( Player ) then Name = Player:Name() end
	self.WPP = { Owned = true , Owner = UniqueID , Name = Name , Time = math.ceil(CurTime()) , Collide = WPP_COLIDE_NORMAL , Used = false }
	self.WPP.Share = { Touch = false , Grab = false , Tool = false , Use = false , Remove = false , Duplicate = false , Damage = false , Noclip = false , CPPI = false }
	return true
end


/*==========================================================================
	Name:		Entity.WPP_IsOwned
	Purpose: 	Returns true if entity is owned
==========================================================================*/
function _R.Entity:WPP_IsOwned()
	if ( self.WPP ) then return (self.WPP.Owned || false) end
	return false
end


/*==========================================================================
	Name:		Entity.WPP_UniqueID
	Purpose: 	Returns the UniqueID of entitys owner
==========================================================================*/
function _R.Entity:WPP_UniqueID()
	if ( self.WPP ) then return self.WPP.Owner end
end


/*==========================================================================
	Name:		Entity.WPP_Owner
	Purpose: 	Returns the owner of this prop as an entity
==========================================================================*/
function _R.Entity:WPP_Owner()
	local UniqueID = self:WPP_UniqueID()
	if ( UniqueID ) then return player.GetByUniqueID( UniqueID ) end
end

/*==========================================================================
	Name:		Entity.WPP_IsOwner
	Purpose: 	Return true if the player owns this entity
==========================================================================*/
function _R.Entity:WPP_IsOwner(Player)
	local UniqueID = self:WPP_UniqueID()
	if ( UniqueID ) then return (UniqueID == Player:UniqueID()) end
	return false
end

/*==========================================================================
	Name:		Entity.WPP_Birth
	Purpose: 	Return the time this entity was created
==========================================================================*/
function _R.Entity:WPP_Birth()
	if ( self.WPP ) then return (self.WPP.Time || 0) end
	return 0
end

/*==========================================================================
	Name:		Entity.WPP_Age
	Purpose: 	Return the age of this entity in seconds
==========================================================================*/
function _R.Entity:WPP_Age()
	return CurTime() - self:WPP_Birth()
end

/*==========================================================================
	Name:		Entity.WPP_Sync
	Purpose: 	Syncs this entitys wpp data with a client
==========================================================================*/
function _R.Entity:WPP_Sync( Player )
	if ( !self.WPP ) then return end
	WPP:SyncEntitys({self},Player)
end

/*==========================================================================
	Name:		Entity.WPP_SetColide
	Purpose: 	Sets this entitys colide mode
==========================================================================*/
function _R.Entity:WPP_SetColide( Int )
	if ( !self.WPP ) then return end
	self.WPP.Collide = Int
end

/*==========================================================================
	Hook:		WPP_SetColide -> WPP
	Purpose: 	Sets this entitys colide mode
==========================================================================*/
hook.Add("WPP_SetColide","WPP",function(Entity,Int)
	Entity:WPP_SetColide( Int )
	
	if ( SERVER ) then
		umsg.Start( "WPP_SC" )
			umsg.Entity( self )
			umsg.Char( Int )
		umsg.End()
	end
end)

/*==========================================================================
	Name:		Entity.WPP_AutoRemoveTime
	Purpose: 	Returns the time till this entity will be removed automaticly
==========================================================================*/
function _R.Entity:WPP_AutoRemoveTime()
	if ( !self:WPP_IsOwned() ) or ( self.WPP.Used ) then return end
	
	local N = GetConVarNumber("wpp_auto_remove")
	if ( N == 0 ) then return end
	return N - self:WPP_Age()
end

/*==========================================================================
	Name:		Entity.WPP_SetShare
	Purpose: 	Sets the share acces of a prop
==========================================================================*/
if ( SERVER ) then
	function _R.Entity:WPP_SetShare(Key,Bool)
		if ( !self.WPP ) then return end
		
		local N
		
		if ( Key == "Touch" ) then
			self.WPP.Share.Touch = Bool
			N = 6
		elseif ( Key == "Grab" ) then
			self.WPP.Share.Grab = Bool
			N = 6
		elseif ( Key == "Tool" ) then
			self.WPP.Share.Tool = Bool
			N = 6
		elseif ( Key == "Use" ) then
			self.WPP.Share.Use = Bool
			N = 6
		elseif ( Key == "Remove" ) then
			self.WPP.Share.Remove = Bool
			N = 6
		elseif ( Key == "Duplicate" ) then
			self.WPP.Share.Duplicate = Bool
			N = 6
		elseif ( Key == "Damage" ) then
			self.WPP.Share.Damage = Bool
			N = 6
		elseif ( Key == "Noclip" ) then
			self.WPP.Share.Noclip = Bool
			N = 6
		elseif ( Key == "CPPI" ) then
			self.WPP.Share.CPPI = Bool
			N = 6
		end
		
		if ( !N ) then return end
		
		umsg.Start("WPP_ES")
			umsg.Char(1)
			umsg.Entity(self)
			
			umsg.Char(N)
			umsg.Bool(Bool)
			
			umsg.Char(0)
		umsg.End()
		
		return true
	end
	
end
